﻿/*************************   Auto Generated Repository Code For EncounterPatientInsurance table     Generated @ 4/25/2013 9:19:13 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class EncounterPatientInsuranceRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an EncounterPatientInsurance by its EncounterPatientInsuranceID (Primary Key)
        /// </summary>
        /// <param name="EncounterPatientInsuranceID">The ID of the <see cref="EncounterPatientInsurance"/> to retrieve</param>
        /// <returns>A <see cref="EncounterPatientInsurance"/> object</returns>
        public BusinessObjects.EncounterPatientInsurance Select(int EncounterPatientInsuranceID)
        {
            EncounterPatientInsurance retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.EncounterPatientInsurances where x.EncounterPatientInsuranceID == EncounterPatientInsuranceID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of EncounterPatientInsurance objects
        /// </summary>
        /// <returns>A List of type <see cref="EncounterPatientInsurance"/></returns>
        public List<BusinessObjects.EncounterPatientInsurance> SelectAll()
        {
            List<BusinessObjects.EncounterPatientInsurance> retObject = new List<BusinessObjects.EncounterPatientInsurance>();

            using (dB = new PracticeSimpleDataContext())
            {
                var EncounterPatientInsuranceList = (from x in dB.EncounterPatientInsurances select x).ToList();

                foreach (EncounterPatientInsurance obj in EncounterPatientInsuranceList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a EncounterPatientInsurance Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public void Save(BusinessObjects.EncounterPatientInsurance businessObject)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                dB.EncounterPatientInsurances.InsertOnSubmit(ConvertToDataObject(businessObject));
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Accepts the Primary Key of a EncounterPatientInsurance, finds it in the database and deletes it
        /// </summary>
        /// <param name="EncounterPatientInsuranceID">The ID of the <see cref="EncounterPatientInsurance"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int EncounterPatientInsuranceID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.EncounterPatientInsurance obj = (from x in dB.EncounterPatientInsurances where x.EncounterPatientInsuranceID == EncounterPatientInsuranceID select x).SingleOrDefault();
                dB.EncounterPatientInsurances.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a EncounterPatientInsurance data object to a EncounterPatientInsurance business object
        /// </summary>
        /// <returns>A business object of type <see cref="EncounterPatientInsurance"/></returns>
        public BusinessObjects.EncounterPatientInsurance ConvertToBusinessObject(DataLayer.EncounterPatientInsurance dataObject)
        {
            BusinessObjects.EncounterPatientInsurance businessObject = new BusinessObjects.EncounterPatientInsurance();

            businessObject.EncounterPatientInsuranceID = dataObject.EncounterPatientInsuranceID;
            businessObject.EncounterID = dataObject.EncounterID;
            businessObject.PatientInsuranceID = dataObject.PatientInsuranceID;
            businessObject.CreatedDate = dataObject.CreatedDate;
            businessObject.CreatedBy = dataObject.CreatedBy;
            businessObject.ModifiedDate = dataObject.ModifiedDate;
            businessObject.ModifiedBy = dataObject.ModifiedBy;


            return businessObject;

        }

        /// <summary>
        /// Converts a EncounterPatientInsurance business object to a EncounterPatientInsurance data object
        /// </summary>
        /// <returns>A data object of type <see cref="EncounterPatientInsurance"/></returns>
        public DataLayer.EncounterPatientInsurance ConvertToDataObject(BusinessObjects.EncounterPatientInsurance businessObject)
        {
            DataLayer.EncounterPatientInsurance dataObject = new DataLayer.EncounterPatientInsurance();

            dataObject.EncounterPatientInsuranceID = businessObject.EncounterPatientInsuranceID;
            dataObject.EncounterID = businessObject.EncounterID;
            dataObject.PatientInsuranceID = businessObject.PatientInsuranceID;
            dataObject.CreatedDate = businessObject.CreatedDate;
            dataObject.CreatedBy = businessObject.CreatedBy;
            dataObject.ModifiedDate = businessObject.ModifiedDate;
            dataObject.ModifiedBy = businessObject.ModifiedBy;


            return dataObject;
        }
    }
}


